﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections;

public partial class product_saving_user_saving_payment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((AfterLogin)this.Master).mainCategory = "TRANSFER";

        String saving_acc = Session["savingAccountNo"] as String;
        String insuranceAccountNo =Request.QueryString["accountNo"];
        String premiumAmount = Request.QueryString["amount"];
        if (null != insuranceAccountNo && "" != insuranceAccountNo)
        {
            this.tbTo.Text = insuranceAccountNo;
        }
        if (null != premiumAmount && "" != premiumAmount)
        {

            this.tbAmount.Text = premiumAmount;

        }

        this.lbAccNo.Text = saving_acc;
       

        

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        AccountsManager am = new AccountsManager();
        
        decimal amount = decimal.Parse(tbAmount.Text);
        string strType = am.getTransactionType(tbTo.Text);
      

        String saving_acc = Session["savingAccountNo"] as String;

        this.lbAccNo.Text = saving_acc;

        
        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection myConnection = new SqlConnection(strConnectionString);
        
        try
        {
            myConnection = DBManager.getSqlConnection();
            myConnection.Open();
            SqlCommand myCommandd = new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);
            
            myCommandd.Parameters.AddWithValue("@date", DateTime.Now);
            myCommandd.Parameters.AddWithValue("@type", strType);
            myCommandd.Parameters.AddWithValue("@status", "SUCCESS");
            myCommandd.Parameters.AddWithValue("@accNo", lbAccNo.Text);
            myCommandd.Parameters.AddWithValue("@transactionAmount", 0-amount);
            myCommandd.ExecuteNonQuery();
            

           
            SqlCommand myCommandi =  new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);
            
            myCommandi.Parameters.AddWithValue("@date", DateTime.Now);
            myCommandi.Parameters.AddWithValue("@type", strType);
            myCommandi.Parameters.AddWithValue("@status", "SUCCESS");
            myCommandi.Parameters.AddWithValue("@accNo", tbTo.Text);
            myCommandi.Parameters.AddWithValue("@transactionAmount", amount);
            myCommandi.ExecuteNonQuery();


            SqlCommand myCommandm = new SqlCommand("update [SAVING_ACCOUNT] SET [balance] = balance-" + amount + " WHERE [accountNo] = " + this.lbAccNo.Text, myConnection);
           
            
            myCommandm.Parameters.AddWithValue("@accountNo", lbAccNo.Text);
            myCommandm.ExecuteNonQuery();
           

            if("INSURANCE PAYMENT".Equals(strType)){
               
                SqlCommand myCommandn = new SqlCommand("update [INSURANCE] SET [balance] = balance+" + amount + " WHERE [accountNo] = '" + this.tbTo.Text+"'" , myConnection);
                myCommandn.ExecuteNonQuery();

                AccountsManager manager = new AccountsManager();
                Hashtable paidOrNot = manager.checkPremiumPaid(this.tbTo.Text);
                foreach (int key in paidOrNot.Keys)
                {
                    bool havePaid = (bool)paidOrNot[key];
                    if (!havePaid)
                    {
                        myCommandn = new SqlCommand("update insured SET paidCount=paidCount+1 WHERE insuredID = " + key, myConnection);
                        myCommandn.ExecuteNonQuery();
                    }
                }

            }
            else if ("FIXED DEPOSIT PAYMENT".Equals(strType))
            {

                SqlCommand myCommandn = new SqlCommand("update [FIXED_DEPOSIT] SET [DepositAmount] = DepositAmount+" + amount + " WHERE [accountNo] = " + this.tbTo.Text, myConnection);
                myCommandn.ExecuteNonQuery();
               
            }

            Label1.Text = "Payment Success, please log in to check for the updated balance. Thank you.";

            myConnection.Close();

            Alert alert = new Alert();
            alert.addAlertOccuredPayment((String)Session["userId"], "You had PAID " + amount + "to " + this.tbTo.Text, "");
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Response.Write(ex);
        }
        finally
        {
            myConnection.Close();
        }
        
    }
}